home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 251_01 / advfio.c < prev    next >
Text File  |  1987-10-27  |  1KB  |  63 lines

  1. /* advfio.c - file i/o routines for the adventure compiler */
  2. /*
  3.     Copyright (c) 1986, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #define BSIZE    8192
  8.  
  9. /* global variables */
  10. long ad_foff;
  11.  
  12. /* external routines */
  13. extern long lseek();
  14.  
  15. /* local variables */
  16. static char buf[BSIZE];
  17. static int boff;
  18. static int fd;
  19.  
  20. ad_create(name)
  21.   char *name;
  22. {
  23.     /* create the file */
  24.     if ((fd = creat(name,0666)) < 0)
  25.     fail("can't create output file");
  26.     
  27.     /* initialize the buffer and file offset */
  28.     ad_foff = 0L;
  29.     boff = 0;
  30. }
  31.  
  32. ad_close()
  33. {
  34.     ad_flush();
  35.     close(fd);
  36. }
  37.  
  38. ad_putc(ch)
  39.   int ch;
  40. {
  41.     buf[boff++] = ch; ad_foff++;
  42.     if (boff >= BSIZE)
  43.     ad_flush();
  44. }
  45.  
  46. ad_seek(pos)
  47.   long pos;
  48. {
  49.     ad_flush();
  50.     if (lseek(fd,pos,0) != pos)
  51.     fail("error positioning output file");
  52.     ad_foff = pos;
  53. }
  54.  
  55. ad_flush()
  56. {
  57.     if (boff) {
  58.     if (write(fd,buf,boff) != boff)
  59.         fail("error writing to output file");
  60.     boff = 0;
  61.     }
  62. }
  63.